<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2020/1/4
// +----------------------------------------------------------------------

namespace app\common\services\statistics;

use app\common\facade\Arr;
use library\tools\Data;
use think\Db;

/**
 *
 * Class HomePageHandleService
 * @package app\common\services\statistics
 * @author feraner<feraner@163.com>
 * @date 2020/1/4
 */
class HomePageHandleService extends BasicHandleService
{

    private $apiHomeSuffix = 'index';

    private $webHomeSuffix = 'index';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $handleData
     *
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function handle($handleData)
    {
        if ($this->isHandle($handleData)) {

            //是否需要增加UV
            $this->addUv = $this->addUvNum(
                Arr::get($handleData, 'userId'),
                Arr::get($handleData, 'logId')
            );


            //首页模块
            $this->__handleHomePageModule($handleData);


        }
    }


    /**
     * 首页模块点击统计
     * @param $handleData
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function __handleHomePageModule($handleData)
    {
        if ($this->isWeb) {
            $dayData = [
                'type'          => 1,
                'index_click'     => Db::raw('index_click + 1'),
                'start_time'    => $this->todayStart,
                'end_time'      => $this->todayEnd,
            ];
            if ($this->addUv) {
                $dayData['index_uv'] = Db::raw('index_uv + 1');
            }

            if ($statId = $this->checkModuleStatExit($dayData)) {
                $dayData['id'] = $statId;
            }

            Data::save('behavior_module_stat', $dayData);

        }
    }


    /**
     * 判断是否需要处理
     * @param $handleData
     *
     * @return bool|mixed
     */
    public function isHandle($handleData)
    {
        $this->isWeb = false;
        $this->isApi = false;

        //api 接口
        if (Arr::valueEq($handleData['urlArr'], 0, $this->apiSuffix) &&
            Arr::valueEq($handleData['urlArr'], 1, $this->apiHomeSuffix)) {
            $this->isApi = true;
            return true;
        }

        //web界面 无具体url
        if (empty($handleData['urlArr'])) {
            $this->isWeb = true;
            return true;
        }

        //web界面
        if (Arr::valueEq($handleData['urlArr'], 0, $this->webSuffix) &&
            Arr::valueEq($handleData['urlArr'], 1, $this->webHomeSuffix)) {
            $this->isWeb = true;
            return true;
        }

        return false;
    }


    /**
     * 系统模块月度 + 总量统计  [所有模块统计聚到一块处理]
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function handleMonthAndTotal()
    {
        //月度数据处理
        $monthResult = Db::name('behavior_module_stat')
            ->where('type', 1)
            ->where('start_time', $this->monthStart)
            ->where('end_time', $this->monthEnd)
            ->field('SUM(job_click) as job_click')
            ->field('SUM(job_uv) as job_uv')
            ->field('SUM(market_click) as market_click')
            ->field('SUM(market_uv) as market_uv')
            ->field('SUM(user_click) as user_click')
            ->field('SUM(user_uv) as user_uv')
            ->field('SUM(index_click) as index_click')
            ->field('SUM(index_uv) as index_uv')
            ->field('SUM(news_click) as news_click')
            ->field('SUM(news_uv) as news_uv')
            ->find() ?: [];

        $monthData = [
            'type'          => 2,
            'job_click'     => Arr::get($monthResult, 'job_click', 1),
            'job_uv'        => Arr::get($monthResult, 'job_uv', 1),
            'market_click'        => Arr::get($monthResult, 'market_click', 1),
            'market_uv'        => Arr::get($monthResult, 'market_uv', 1),
            'user_click'        => Arr::get($monthResult, 'user_click', 1),
            'user_uv'        => Arr::get($monthResult, 'user_uv', 1),
            'index_click'        => Arr::get($monthResult, 'index_click', 1),
            'index_uv'        => Arr::get($monthResult, 'index_uv', 1),
            'news_click'        => Arr::get($monthResult, 'news_click', 1),
            'news_uv'        => Arr::get($monthResult, 'news_uv', 1),
            'start_time'    => $this->monthStart,
            'end_time'      => $this->monthEnd,
        ];

        if ($monthStatId = $this->checkModuleStatExit($monthData)) {
            $monthData['id'] = $monthStatId;
        }
        Data::save('behavior_module_stat', $monthData);

        //总量数据处理
        $totalResult = Db::name('behavior_module_stat')
            ->where('type', 2)
            ->field('SUM(job_click) as job_click')
            ->field('SUM(job_uv) as job_uv')
            ->field('SUM(market_click) as market_click')
            ->field('SUM(market_uv) as market_uv')
            ->field('SUM(user_click) as user_click')
            ->field('SUM(user_uv) as user_uv')
            ->field('SUM(index_click) as index_click')
            ->field('SUM(index_uv) as index_uv')
            ->field('SUM(news_click) as news_click')
            ->field('SUM(news_uv) as news_uv')
            ->find() ?: [];

        $totalData = [
            'type'          => 3,
            'job_click'     => Arr::get($totalResult, 'job_click', 1),
            'job_uv'        => Arr::get($totalResult, 'job_uv', 1),
            'market_click'  => Arr::get($totalResult, 'market_click', 1),
            'market_uv'     => Arr::get($totalResult, 'market_uv', 1),
            'user_click'    => Arr::get($totalResult, 'user_click', 1),
            'user_uv'       => Arr::get($totalResult, 'user_uv', 1),
            'index_click'   => Arr::get($totalResult, 'index_click', 1),
            'index_uv'      => Arr::get($totalResult, 'index_uv', 1),
            'news_click'    => Arr::get($totalResult, 'news_click', 1),
            'news_uv'       => Arr::get($totalResult, 'news_uv', 1),
            'start_time'    => 0,
            'end_time'      => 0,
        ];

        if ($totalStatId = $this->checkModuleStatExit($totalData)) {
            $totalData['id'] = $totalStatId;
        }
        Data::save('behavior_module_stat', $totalData);


    }

}
